<?php

class account{
	var $idUser;
	var $username;
	var $password;
	var $nama_lengkap;
	var $email;
	var $jenis_aktivity;
	var $date_login;
	var $foto_profil;
	
	public function __construct(){
		$this->username = '';
		$this->password = '';
	}
	
	public function set_user($username){
		$this->username = $username;
	}
	
	public function set_password($password){
		$this->password = $password;
	}
	
	public function do_login(){
		$db = new DBClass();
		$query = "Select * from user where username='$this->username' and password=md5('$this->password')";
		$rows = $db->getRows($query);
		$date_login = date('d-m-y h:i:s');
		$jenis_aktivity = "login";
		if($rows!=null){
			$this->idUser = $rows[0]['id_user'];
			$this->nama_lengkap = $rows[0]['nama_lengkap'];
			$this->email = $rows[0]['email'];

			$this->foto_profil = $rows[0]['foto_profil'];

			$this->set_cookie();	
			$db->putRows("INSERT INTO log_activity_login (jenis_aktivity,user_id_user,datetime_activity) VALUES('".$jenis_aktivity."',".$rows[0]['id_user'].",'".$date_login."')");
			return true;
		}else{
			return false;
		}
		
	}
	
	public function do_logout(){
		$this->delete_cookie();
	}
	
	protected function set_cookie(){
		setcookie("c_id_user", $this->idUser); 
		setcookie("c_usr", $this->username); 
		setcookie("c_pwd", md5($this->password)); 
		setcookie("c_name", $this->nama_lengkap); 
		setcookie("c_email", $this->email);
		setcookie("c_foto_profil", $this->foto_profil); 
	}
	
	public function get_cookie(){
		return $_COOKIE;
	}
	
	public function validate_cookie(){
		if(@$_COOKIE['c_usr'] == '' || @$_COOKIE['c_pwd'] == '')
			return false;
		else return true;
	}

	protected function delete_cookie(){
		setcookie("c_id_user", '');
		setcookie("c_usr", ''); 
		setcookie("c_pwd", ''); 
		setcookie("c_name", ''); 
		setcookie("c_email", '');
		setcookie("c_foto_profil", '');

	}

}